package com.qxy.practice.reflect;

import lombok.AllArgsConstructor;
import cn.hutool.core.io.FileUtil;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.lang.reflect.Method;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author : 戚欣扬
 * @code : 基于反射的自动测试框架
 */

public class AutoTestFrame {
    private Object object;

    private AtomicInteger methodCount;

    private AtomicInteger exceptionCount;

    public AutoTestFrame() {
        methodCount = new AtomicInteger(0);
        exceptionCount = new AtomicInteger(0);
    }

    public void setTarget(Object object) {
        this.object = object;
    }

    public void test (){
        Method[] methods = object.getClass().getMethods();
        for (Method method : methods) {
            if(method.isAnnotationPresent(MyAnnotation.class)) {
                try {
                    method.invoke(object, 10, 0);
                } catch (Exception e) {
                    exceptionCount.incrementAndGet();
                    System.out.println("方法" + method.getName() + "执行失败");
                }
            }
            methodCount.incrementAndGet();
        }

        System.out.println("一共执行了"+methodCount.get()+"个方法");
        System.out.println("一共出现了"+exceptionCount.get()+"个异常");
    }
}


